//
//  UserModelDataSourceProtocolTests.swift
//  travelMapMvvm-swift2
//
//  Created by green on 15/11/3.
//  Copyright © 2015年 travelMapMvvm-swift2. All rights reserved.
//

import Foundation
import XCTest
import ReactiveCocoa
import TMDatasourceLib
import TMCommonLib

/**
 问题记录：
 1、注册返回用户信息？
 2、修改用户信息接口是否可以直接修改用户头像url
 */

class UserModelDataSourceProtocolTests: XCTestCase {
    
    var userModelDataSourceProtocol: UserModelDataSourceProtocol!
    
    override func setUp() {
        super.setUp()
        
        userModelDataSourceProtocol = UserModelDataSource.shareInstance()
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
    /**
     手机登录测试
     */
    func testTelephoneLogin() {
        
        let expectation = expectationWithDescription(kURL + "telephonelogin")
        
        userModelDataSourceProtocol.telephoneLogin("13917470145", password: "1").subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is UserModel,"response obj is not UserModel")
            
            print(any as? UserModel)
            
        }, error: { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTFail(error.localizedDescription)
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
    
    /**
     保存登录页面默认显示的手机号测试
     查询登录页面默认显示的手机号测试
     */
    func testQueryLoginPageDefaultTelephone() {
        
        userModelDataSourceProtocol.saveLoginPageDefaultTelephone("13917470145")
        let telephone = userModelDataSourceProtocol.queryLoginPageDefaultTelephone()
        XCTAssert(telephone == "13917470145", "telephone value is not failure")
        
        print(telephone)
    }
    
    /**
     保存登录用户信息测试
     查询登录用户信息测试
     清除登录用户信息
     */
    func testSaveUserAndQueryUserAndClearUser() {

        let user = UserModel(data: ["userId":12,"userName":"testName","userPicUrl":"http://www.baidu.com","telephone":"13917470145","email":"chenchangqing198@126.com"])
        userModelDataSourceProtocol.saveUser(user)
        var tempU = userModelDataSourceProtocol.queryUser()
        print(tempU)
        XCTAssertNotNil(tempU, "save user and query user failure")
        
        userModelDataSourceProtocol.clearUser()
        tempU = userModelDataSourceProtocol.queryUser()
        print(tempU)
        XCTAssertNil(tempU, "clear user success")
        
    }
    
    /**
     手机注册测试
     */
    func testTelephoneRegister() {
        
        let expectation = expectationWithDescription(kURL + "telephoneregister")
        
        userModelDataSourceProtocol.telephoneRegister("13917471221", password: "1").subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is UserModel,"response obj is not UserModel")
            
            print(any as? UserModel)
            
        }, error: { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTAssert(error.localizedDescription != DataError.TelephoneRegisterError(nil).error.localizedDescription, "telephone register failure:\(error.localizedDescription)")
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
        
    }
    
    /**
     忘记密码测试
     */
    func testForgetPwd() {
        
        let expectation = expectationWithDescription(kURL + "resetpwd")
        
        userModelDataSourceProtocol.forgetPwd("13917470145", password: "1").subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(kSuccess==any as? String, "reset pwd failure")
        }, error: { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTFail(error.localizedDescription)
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
    
    /**
     修改用户信息测试
     */
    func testModifyUInfo() {
        
        let expectation = expectationWithDescription(kURL + "modifyuserinfo")
        
        let newUserName = "testName"
        let newEmail = "changqing198@126.com"
        let newUserPicURL = "http://xxx.jpg"
        
        userModelDataSourceProtocol.modifyUInfo(4610549, userName: newUserName, email: newEmail, mobile: "13917470145", userPicUrl:newUserPicURL).subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is UserModel,"response obj is not UserModel")
            XCTAssert((any as! UserModel).userPicUrl?.absoluteString == newUserPicURL, "user pic url modify failure")
            XCTAssert((any as! UserModel).userName == newUserName, "user name modify failure")
            XCTAssert((any as! UserModel).email == newEmail, "user email modify failure")
            
            print(any as! UserModel)
            
        }, error: { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTFail(error.localizedDescription)
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })

    }
    
    /**
     本地头像是否存在测试
     */
    func testLocalTouxiangExist() {
        
        let touxiang = UIImage(named: "touxiang.jpg")
        
        XCTAssertNotNil(touxiang, "test image is nil")
        
    }
    
    /**
     上传头像测试
     */
    func testUploadHeadImage() {
        
        let touxiang = UIImage(named: "touxiang.jpg")
        
        let expectation = expectationWithDescription(kURL + "uploadphoto")
        
        userModelDataSourceProtocol.uploadHeadImage(4610549, headImage: touxiang!).subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is NSURL,"response obj is not NSURL")
            
            print(any as? NSURL)
            
        }, error: { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTFail(error.localizedDescription)
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
    
}